//
//  YRPictureView.swift
//  新浪微博
//
//  Created by Viper on 16/6/30.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit

private let itemMargin : CGFloat = 5
//计算cell的宽高
private let cellWH = (SCREENW - StatusMargin * 2 - itemMargin * 2)/3

private let pictureViewCellId = "pictureViewCellId"

class YRPictureView: UICollectionView {
 
    var pic_urls : [YRPicrureInfo]?{
        
        didSet{
         
            // 给countLabel设置text
//            lable.text = "\(pic_urls?.count ?? 0)"
            let size = getPictureViewSize(pic_urls?.count ?? 0)
            // 给其设置约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            // 刷新UI D(当拿到数据 第一时间要刷新)
            reloadData()

        }
    
    }
    func getPictureViewSize(count:Int) -> CGSize{
        
       
        // 计算行数 和列数
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
        // 计算配图宽高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*itemMargin
        
        return CGSize(width: w, height: h)
    }


    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        backgroundColor = UIColor.clearColor()
     
//        addSubview(lable)
//        
//        lable.snp_makeConstraints { (make) in
//            make.center.equalTo(self)
//        }
        //注册cell

        registerClass(YRPictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellId)
        
        dataSource = self
        
        //设置layout
        let layout = collectionViewLayout as!UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumLineSpacing = itemMargin
        layout.minimumInteritemSpacing = itemMargin
        
        
        
        }
    
//    private lazy var lable : UILabel = UILabel(textColor: UIColor.redColor(), textFont: 30)
    
        
    }

extension YRPictureView : UICollectionViewDataSource{
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellId, forIndexPath: indexPath) as!YRPictureViewCell
        
//        cell.backgroundColor = RandomColor()
        
          cell.picInfo = pic_urls![indexPath.item]
        
        return cell
        
    }
}
//自定义cell
class YRPictureViewCell: UICollectionViewCell {
    //供外界赋值
    var picInfo : YRPicrureInfo?{
        
        didSet{
            //设置图片
            bgImageView.sd_setImageWithURL(NSURL(string: picInfo?.thumbnail_pic ?? "" ), placeholderImage: UIImage(named: "timeline_card_top_background"))
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
      setuoUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置UI
    
    private func setuoUI(){
        
        backgroundColor = UIColor.clearColor()
        
        //添加控件
        contentView.addSubview(bgImageView)
        
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        
    }
    //MARK: - 懒加载控件
    private lazy var bgImageView : UIImageView = {
       
        let image = UIImageView(imageName: "timeline_card_top_background")
        //指定图片填充方式
        image.contentMode = .ScaleAspectFill
        
        image.clipsToBounds = true

        return image
        
    }()
    
}

